package com.chainshop.restock.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.chainshop.common.core.domain.pojo.Restock;
import com.chainshop.common.core.domain.pojo.StoreInfo;
import com.chainshop.restock.constant.RestockConstants;
import com.chainshop.restock.service.IRestockStoreService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chainshop.common.annotation.Log;
import com.chainshop.common.core.controller.BaseController;
import com.chainshop.common.core.domain.AjaxResult;
import com.chainshop.common.enums.BusinessType;
import com.chainshop.restock.service.IRestockService;
import com.chainshop.common.utils.poi.ExcelUtil;
import com.chainshop.common.core.page.TableDataInfo;

/**
 * 存储补货请求的Controller
 * 
 * @author ruoyi
 * @date 2024-08-08
 */
@RestController
@RequestMapping("/restock/restock")
public class RestockController extends BaseController
{
    @Autowired
    private IRestockService restockService;

    @Autowired
    private IRestockStoreService restockStoreService;

    /**
     * 查询存储补货请求的列表
     */
    @PreAuthorize("@ss.hasPermi('restock:restock:list,dispatch:list')")
    @GetMapping("/list")
    public TableDataInfo list(Restock restock)
    {
        System.out.println("restock:"+restock);
        startPage();
        List<Restock> list = restockService.selectRestockList(restock);
        return getDataTable(list);
    }

    /**
     * 导出存储补货请求的列表
     */
    @PreAuthorize("@ss.hasPermi('restock:restock:export')")
    @Log(title = "存储补货请求的", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Restock restock)
    {
        List<Restock> list = restockService.selectRestockList(restock);
        ExcelUtil<Restock> util = new ExcelUtil<Restock>(Restock.class);
        util.exportExcel(response, list, "存储补货请求的数据");
    }

    /**
     * 获取存储补货请求的详细信息
     */
    @PreAuthorize("@ss.hasPermi('restock:restock:query,dispatch:query')")
    @GetMapping(value = "/{replenishId}")
    public AjaxResult getInfo(@PathVariable("replenishId") Long replenishId)
    {
        return success(restockService.selectRestockByReplenishId(replenishId));
    }

    /**
     * 新增存储补货请求的
     */
    @PreAuthorize("@ss.hasPermi('restock:restock:add,dispatch:add')")
    @Log(title = "存储补货请求的", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Restock restock)
    {
        return toAjax(restockService.insertRestock(restock));
    }

    /**
     * 修改存储补货请求的
     */
    @PreAuthorize("@ss.hasPermi('restock:restock:edit,dispatch:edit')")
    @Log(title = "存储补货请求的", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Restock restock)
    {
        return toAjax(restockService.updateRestock(restock));
    }

    /**
     * 删除存储补货请求的
     */
    @PreAuthorize("@ss.hasPermi('restock:restock:remove,dispatch:remove')")
    @Log(title = "存储补货请求的", businessType = BusinessType.DELETE)
	@DeleteMapping("/{replenishIds}")
    public AjaxResult remove(@PathVariable Long[] replenishIds)
    {
        return toAjax(restockService.deleteRestockByReplenishIds(replenishIds));
    }

    @PreAuthorize("@ss.hasPermi('restock:restock:query,dispatch:query')")
    @Log(title = "存储补货请求的", businessType = BusinessType.DELETE)
    @GetMapping("getDetail/{replenishId}")
    public AjaxResult getDetailList(@PathVariable Long replenishId)
    {
        Restock restock = restockService.selectRestockDetailById(replenishId);
        return AjaxResult.success(restock);
    }

    @PreAuthorize("@ss.hasPermi('restock:restock:add,dispatch:add')")
    @Log(title = "存储补货请求的", businessType = BusinessType.INSERT)
    @GetMapping("addDetail/{storeId}")
    public AjaxResult upDataDetailList(@PathVariable Integer storeId)
    {
        StoreInfo storeInfo = restockStoreService.selectStoreById(storeId);
        Restock restock = new Restock();
        restock.setStoreName(storeInfo.getStoreName());
        restock.setStoreId(storeId);
        restock.setSubmitTime(new Date());
        restock.setManagerName(storeInfo.getManagerName());
        return AjaxResult.success(restock);
    }

    @PreAuthorize("@ss.hasPermi('restock:restock:add,dispatch:add')")
    @PostMapping("/send")
    public AjaxResult send(@RequestBody Restock restock){
        int rowRes = 0;
        if (StrUtil.isBlank(restock.getStatus())){
            restock.setStatus(RestockConstants.REPLENISH_STATUS_WAIT_AUDIT);
            rowRes = restockService.insertRestock(restock);
        }else if (!RestockConstants.REPLENISH_STATUS_AUDIT.equals(restock.getStatus())) {
            rowRes = restockService.updateRestock(restock);
        }
        return toAjax(rowRes);
    }

    @PreAuthorize("@ss.hasPermi('restock:restock:add,dispatch.add')")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Restock restock){
        int rowRes = 0;
        System.out.println(restock);
        if (StrUtil.isBlank(restock.getStatus())) {
            restock.setStatus(RestockConstants.REPLENISH_STATUS_SAVE);
            rowRes = restockService.insertRestock(restock);
        }else if (RestockConstants.REPLENISH_STATUS_SAVE.equals(restock.getStatus())) {
            rowRes = restockService.updateSaveRestock(restock);
        }
        return toAjax(rowRes);
    }

    @PreAuthorize("@ss.hasPermi('restock:restock:add,dispatch:add')")
    @PostMapping("/submit")
    public AjaxResult submitRestock(@RequestBody Restock restock){
        System.out.println(restock);
        int rowRes = 0;
        if (StrUtil.isNotBlank(restock.getStatus()) && RestockConstants.REPLENISH_STATUS_WAIT_AUDIT.equals(restock.getStatus())) {
            rowRes = restockService.updateSubmitRestock(restock);
        }
        return toAjax(rowRes);
    }
}
